An Armstrong number (also known as narcissistic number or plenary number) is a number that is equal to the sum of its own digits raised to the power of the number of digits.
def is_armstrong_number(number):
num_str = str(number)
num_digits = len(num_str)
armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
return armstrong_sum == number
# Taking input from the user
number = int(input("Enter a number to check if it's an Armstrong number: "))
# Checking if the number is an Armstrong number
if is_armstrong_number(number):
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")
Enter a number to check if it's an Armstrong number: 153
153 is an Armstrong number.
Enter a number to check if it's an Armstrong number: 123
123 is not an Armstrong number.
The function is_armstrong_number(number)
checks whether a given number is an Armstrong number or not.
It calculates the sum of the digits of the number raised to the power of the number of digits. If this sum equals the original number, then the number is an Armstrong number.
The example usage section demonstrates how to take input from the user, check if the number is an Armstrong number, and print the result.